package com.plugin.sproutcore.util;

import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleConstants;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.IConsoleView;
import org.eclipse.ui.console.MessageConsole;

public class ConsoleUtil 
{
	private static MessageConsole console;
	private static final String name = "Sproutcore Log";
	
	public static MessageConsole getConsole()
	{
		if (console == null) 
		{
			//no console found, so create a new one
			ConsolePlugin plugin = ConsolePlugin.getDefault();
			IConsoleManager conMan = plugin.getConsoleManager();
			console = new MessageConsole(name, null);
			conMan.addConsoles(new IConsole[]{console});
		}
		return console;
	}
	
	public static void displayConsole(IWorkbenchPage page)
	{
		String id = IConsoleConstants.ID_CONSOLE_VIEW;
		IConsoleView view;
		try {
			view = (IConsoleView) page.showView(id);
			view.display(getConsole());
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}
}
